package fabworkz.anna;

import java.util.ArrayList;

import root.gast.speech.activation.SpeechActivationService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class SpeechRecognitionLauncher extends Activity {
	private static final String TAG = "SpeechRecognitionLauncher";
	private static final int VOICE_RECOGNITION_REQUEST_CODE = 101010;
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
//        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//        PowerManager.WakeLock lck = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
//        lck.acquire(1000);
        Window window = getWindow();  
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        //sleep a bit to wait for screen to wake up
//        try {
//			Thread.sleep(5000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

    }
    @Override
    protected void onStart(){
    	super.onStart();
        Intent recognizerIntent =
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Listening ..");
        recognizerIntent.setFlags(Intent.FLAG_FROM_BACKGROUND);
        startActivityForResult(recognizerIntent,VOICE_RECOGNITION_REQUEST_CODE);
    }
    @Override
    protected void
            onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK)
            {
	            ArrayList<String> matches = data.getStringArrayListExtra(
	                    RecognizerIntent.EXTRA_RESULTS);
	            if(matches.get(0).equals("Google")){
	            	Log.d("ANNA", "heard Google, launching voice search..");
	            	Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
	            	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//	            	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//	            	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
	            	startActivity(intent);
	            }
//                Intent showResults = new Intent(data);
//                showResults.setClass(this,
//                        SpeechRecognitionResultsActivity.class);
//                startActivity(showResults);
            	Log.d(TAG,"result ok");
//	            Intent stopService = SpeechActivationService.makeServiceStopIntent(this);
//	            startService(stopService);
//	            try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
            }
        }
        Intent i = SpeechActivationService.makeStartServiceIntent(this,
                "clapper",8000);
        startService(i);

        finish();
    }
}
